<?php

class ChatController
{
    private $chatModel;
    private $userStaticModel;
    private $autoUidModel;

    function __construct()
    {
        $this->chatModel    =   new ChatModel();
        $this->userStaticModel =new UserStatisticsModel();
        $this->autoUidModel =   new AutoUidModel();
    }

    public function postChat($info)
    {
        // 是否禁言
        $status  =  $this->autoUidModel->checkUserStatus(AC::$hostUserBaseInfo);
        if (!empty(AC::$errorNo)){
            return  FALSE;
        }
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'CC001';
            return  FALSE;
        }
        if (AC::$hostUserBaseInfo['uid'] != $info['uid'])
        {
            AC::$errorNo    =   'CC002';
            return  FALSE;
        }

        $info['nickname']   =   AC::$hostUserBaseInfo['nickname'];

        $result =   $this->chatModel->postChat($info);

        if (empty(AC::$errorNo))
        {
            unset($info['uid']);
            unset($info['nickname']);
            unset($info['content']);
            unset($info['device']);
            if (!empty($info['id']))    $result =   $this->getChatList($info);
        }
        return  $result;
    }

    public function getChatList($info)
    {
        #   if (empty($info['id'])  ||  $info['id'] <   841243) $info['id'] =   841243;

        if (empty($info['listMode']))
        {
            $chatList   =   $this->chatModel->getChatList($info);
        }
        else
        {
            if ('up' == $info['listMode'])
            {
                $chatList   =   $this->chatModel->getChatListUp($info);
            }
            else
            {
                $chatList   =   $this->chatModel->getChatListDown($info);
            }
        }/*}}}*/
        // 聊天室增加用户头像
        if (!empty($chatList)){
            foreach ($chatList as &$value ){
               $userInfo =  $this->autoUidModel->getUserInfo($value['uid']);
               $value['avatar']    =    AC::getProtocolUriByOS($userInfo['avatar']);
               $value['nickname']    =    $this->autoUidModel->getRemarkName($userInfo);
            }
        }
        // 重置情感天地轮询
        $this->userStaticModel->resetNewChatMessageTimesStatistics(array('uid'=>AC::HOST_UID));
        return  $chatList;
    }

    public function removeChat($info)
    {
        if (empty($info['id']))
        {
            AC::$errorNo    =   'CC003';
            return  FALSE;
        }

        $result =   $this->chatModel->removeChat($info);
        return  $result;
    }

    public function modifyChat($info)
    {
        if (empty($info['id']))
        {
            AC::$errorNo    =   'CC003';
            return  FALSE;
        }

        $result =   $this->chatModel->removeChat($info);
        return  $result;
    }
}


